class Block {
  constructor(shape) {
    this.shape = shape;
    this.position = { x: 0, y: 0 };
    this.rotation = 0;
    this.lastShape = []; // 用于存储上一次的形状
    this.lastRotation = 0; // 用于存储上一次的旋转状态
  }

  // 正确的旋转形状逻辑
  rotate() {
    // 保存上一次的状态
    this.lastShape = this.shape.slice();
    this.lastRotation = this.rotation;

    if (this.rotation === 3) {
      this.rotation = 0; // 重置为0°
    } else {
      this.rotation += 1; // 增加旋转角度
    }

    // 根据当前旋转角度来旋转形状
    const newShape = [];
    for (let i = 0; i < this.shape[0].length; i++) {
      newShape.push([]);
      for (let j = this.shape.length - 1; j >= 0; j--) {
        newShape[i].push(this.shape[j][i]);
      }
    }

    this.shape = newShape;
  }

  // 撤销旋转
  undoRotate() {
    if (this.lastShape.length > 0) {
      this.shape = this.lastShape;
      this.rotation = this.lastRotation;
      this.lastShape = []; // 清空上一次的状态
    } else {
      console.log("No rotation history to undo");
    }
  }

  // 移动形状
  move(dx, dy) {
    this.position.x += dx;
    this.position.y += dy;
  }
}

// 定义七种基本形状
const shapes = [
new Block([[2, 0], [0, 2], [2, 2]]), // S
new Block([[0, 2], [2, 2], [2, 0]]), // Z
new Block([[2, 2, 2, 2]]), // I
new Block([[2, 2], [2, 2]]), // O
new Block([[0, 2, 0], [2, 2, 2], [0, 0, 0]]), // T
new Block([[2, 2, 2], [0, 0, 2]]), // L
new Block([[2, 2, 2], [2, 0, 0]]) // J
];

export { Block, shapes };